//
//  AuthRepository.swift
//  
//
//  Created by tank on 2021/11/16.
//

import Foundation
import LibBase
import LibNetwork

public class AuthRepository: DataRepository<AuthModel> {
    public override class var id: String {
        return "AuthRepository"
    }
    
    public required init() {
        super.init(data: ServiceManager.shared.cacheService
            .globalCache
            .objectNSSecureCoding(type: AuthModel.self,
                                  key: AuthRepository.id))
    }
    
    public override func save() {
        guard let saveData = self.data else {
            ServiceManager.shared.cacheService
                .globalCache
                .remove(key: AuthRepository.id)
            return
        }
        
        ServiceManager.shared.cacheService
            .globalCache
            .setObjectNSSecureCoding(value: saveData, key: AuthRepository.id)
    }
    
    func update(accessToken: String, refreshToken: String, expiredTime: Date) {
        guard let data = data else {
            return
        }
        
        data.accessToken = accessToken
        data.refreshToken = refreshToken
        data.expiredTime = expiredTime
        
        set(data: data)
    }
}
